package com.wac.data.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.data.mapper.UserMapper;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class DataListUtil {

	//list封装
	public Map<String, Object> data(Page thePage) {
		Map<String, Object> map = new HashMap<>();
		map.put("data", thePage.getRecords());
		map.put("page", thePage.getCurrent());
		map.put("size", thePage.getSize());
		map.put("total", thePage.getTotal());
		return map;
	}

	//等级晋升制度
	public boolean upLevel(UserMapper userMapper, String id) {
		//该用户当前等级
		int level = userMapper.selectById(id).getLevel();
		//该用户当前总经验值
		int Exp = userMapper.selectById(id).getExp();

		if (level < 9 && Exp >= 140 + (level * 150)) {
			if (userMapper.addLevel(id) == 1) {
				System.out.println("用户" + id + "成功升级1");
				upLevel(userMapper, id);
			}
		}
		if (level >= 9 && level < 29 && Exp >= level * 250 - 500) {
			if (userMapper.addLevel(id) == 1) {
				System.out.println("用户" + id + "成功升级1");
				upLevel(userMapper, id);
			}
		}
		if (level >= 29 && Exp >= level * 500 - 5000) {
			if (userMapper.addLevel(id) == 1) {
				System.out.println("用户" + id + "成功升级1");
				upLevel(userMapper, id);
			}
		}
		return true;
	}

	//获得cookies
	public String getCookies(HttpServletRequest request) {
		String token = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null && cookies.length > 0) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("user")) {
					token = cookie.getValue();
				}
			}
		}
		return token;
	}
}
